package com.ubs.training.server.dao.impl.jdbc;

import com.ubs.training.server.dao.RatingAgencyDao;
import com.ubs.training.server.dao.model.RatingAgency;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/21/12
 * Time: 5:00 AM
 * To change this template use File | Settings | File Templates.
 */
public class NamedParamRatingAgencyDaoImpl implements RatingAgencyDao {

    DataSource ds;

    NamedParameterJdbcOperations ops;


    public DataSource getDs() {
        return ds;
    }

    public void setDs(DataSource ds) {
        this.ds = ds;
        ops = new NamedParameterJdbcTemplate(ds);
    }

    @Override
    public List<RatingAgency> getAll() {
        return ops.query("select RatingAgencyID, RatingAgencyName from RatingAgency", Collections.EMPTY_MAP, new BeanPropertyRowMapper(RatingAgency.class));
    }

    @Override
    public RatingAgency getById(int id) {
        return ops.queryForObject("select RatingAgencyID, RatingAgencyName from RatingAgency where id=:RatingAgencyID", Collections.singletonMap("RatingAgencyID", id), new BeanPropertyRowMapper<RatingAgency>(RatingAgency.class));
    }

    @Override
    public void add(RatingAgency ratingAgency) {
        int id = ops.queryForInt("select next value for cmstr_seq from dual", Collections.EMPTY_MAP);
        ratingAgency.setRatingAgencyID(id);
        ops.update("insert into RatingAgency values (:RatingAgencyID, :RatingAgencyName)", new BeanPropertySqlParameterSource(ratingAgency));
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void update(RatingAgency ratingAgency) {


        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void delete(RatingAgency ratingAgency) {
    }

}
